//
//  AWWelcomeController.swift
//  WeiBo
//
//  Created by AaronWei on 2017/6/7.
//  Copyright © 2017年 AaronWei. All rights reserved.
//

import UIKit
import SnapKit
import SDWebImage

class AWWelcomeController: UIViewController {
    
    var userName: String?
    
    fileprivate lazy var backgroundImageView = UIImageView(image: #imageLiteral(resourceName: "ad_background"))
    
    fileprivate lazy var userImageView: UIImageView = {
    
        let imageView = UIImageView(image: #imageLiteral(resourceName: "avatar_default_big"))
        
        imageView.layer.cornerRadius = 45
        
        imageView.layer.masksToBounds = true
        
        if let imageUrl = AWUserAccountViewModel.sharedViewModel.userAccount?.avatar_large {
            
            imageView.sd_setImage(with: URL(string: imageUrl), placeholderImage: #imageLiteral(resourceName: "avatar_default_big"))
            
        }
        
        return imageView
        
    }()
        
    fileprivate lazy var messageLabel: UILabel = {
    
        let label = UILabel(title: "欢迎回来", fontSize: 14, textColor: UIColor.darkGray, maxWidth: nil)
        
        if let name = AWUserAccountViewModel.sharedViewModel.userAccount?.name {
            
            label.text = "欢迎回来，\(name)"
            
        } else {
            
            label.text = "欢迎回来"
            
        }
        
        label.alpha = 0
        
        return label
    
    }()
    
    override func loadView() {
        
        view = backgroundImageView
        
    }

    override func viewDidLoad() {
        super.viewDidLoad()

        setupUI()
        
    }
    
    override func viewDidAppear(_ animated: Bool) {
        
        super.viewDidAppear(animated)
        
        startAnimation()
        
    }

    private func setupUI() {
        
        view.addSubview(userImageView)
        
        view.addSubview(messageLabel)
        
        userImageView.snp.makeConstraints { (make) in
            
            make.top.equalTo(self.view).offset(200)
            make.centerX.equalTo(self.view)
            make.size.equalTo(CGSize(width: 90, height: 90))
            
        }
        
        messageLabel.snp.makeConstraints { (make) in
            
            make.top.equalTo(userImageView.snp.bottom).offset(20)
            make.centerX.equalTo(userImageView)
            
        }
        
    }
    
    private func startAnimation() {
        
        userImageView.snp.updateConstraints { (make) in
            
            make.top.equalTo(self.view).offset(100)
            
        }
        
        UIView.animate(withDuration: 1, delay: 0, usingSpringWithDamping: 0.6, initialSpringVelocity: 10, options: [], animations: { 
            
            self.view.layoutIfNeeded()
            
        }) { (_) in
            
            UIView.animate(withDuration: 1, animations: { 
                
                self.messageLabel.alpha = 1
                
            }, completion: { (_) in
                
                UIView.animate(withDuration: 1, animations: {
                    
                    self.view.transform = CGAffineTransform.init(scaleX: 1.5, y: 1.5)
                    
                    self.view.alpha = 0
                    
                }, completion: { (_) in
                                                            
                    NotificationCenter.default.post(name: Notification.Name(SwitchRootVCNotificationName), object: nil)
                    
                })
                
                
                
            })
            
        }
        
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
}
